package cn.edu.eight.food.controller;

import cn.edu.eight.food.dao.OfCommentMapper;
import cn.edu.eight.food.model.OfComment;
import cn.edu.eight.food.model.OfUser;
import cn.edu.eight.food.service.ofCommentService;
import cn.edu.eight.food.util.Result;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/comment")
public class ofCommentController {
    @Autowired
    ofCommentService ofCommentService;

    @RequestMapping("/commentList")
    private String commentList(HttpServletRequest request){
        List<Map> mapList=ofCommentService.findCommentAll();
        request.setAttribute("mapList",mapList);
        return "evaluate";
    }

    @RequestMapping("/addComment")
    @ResponseBody
    public Result addComment(@RequestBody OfComment ofComment, HttpSession session){
        Result result = new Result();
        OfUser user=(OfUser) session.getAttribute("loginUser");
        if (user == null) {
            result.setCode("500");
            result.setMsg("请先登录！");
            return result;
        }else {
            ofComment.setOfCommentUserId(user.getOfUserId());
            ofComment.setOfCommentUserName(user.getOfUserName());
            ofCommentService.insert(ofComment);
            result.setCode("200");
            return result;
        }
    }
}
